import {
	storage
} from '@/utils/cache'
import Vue from 'vue'

const types = {
	luckyCodeList: 'luckyCodeList', // 幸运码列表
	participantList: 'participantList', // 参与用户列表
	drawList: 'drawList', // tabBar页抽奖列表
	bargainList: 'bargainList', // tabBar页砍价列表
	bargainRecordList: 'bargainRecordList', // 砍价记录列表
}

const state = {
	[types.luckyCodeList]: [],
	[types.participantList]: [],
	[types.drawList]: null,
	[types.bargainList]: null,
	[types.bargainRecordList]: [],
}

const getters = {
	_luckyCodeList(state) {
		return state[types.luckyCodeList]
	},
	_participantList(state) {
		return state[types.participantList]
	},
	_drawList(state) {
		return state[types.drawList]
	},
	_bargainList(state) {
		return state[types.bargainList]
	},
	_bargainRecordList(state) {
		return state[types.bargainRecordList]
	},
}

const actions = {
	[types.luckyCodeList + 'Up']: async ({
		commit,
		state,
		dispatch
	}, list = []) => {
		return commit(types.luckyCodeList, list)
	},
	[types.participantList + 'Up']: async ({
		commit,
		state,
		dispatch
	}, list = []) => {
		return commit(types.participantList, list)
	},
	[types.drawList + 'Load']: async ({
		commit,
		state,
		dispatch
	}, list = []) => {
		try {
			let data = await Vue.prototype.$api.draw.getDrawPrizeList({});

			const newList = data.list || [];
			newList.map((item) => {
				if (item.lotteryTime) {
					const arr = Vue.prototype.$util.formatTimestamp(item.lotteryTime)
					item.lotteryTime = arr[1] + '月' + arr[2] + '日 ' + arr[3] + ':' + arr[4];
				}
			})
			commit(types.drawList, newList)
			return newList
		} catch (e) {
			console.log('request fail', e);
			return
		}
	},
	[types.bargainList + 'Load']: async ({
		commit,
		state,
		dispatch
	}) => {
		try {	
			let newList = await Vue.prototype.$api.bargain.getBargainPrizeList({}) || [];
			newList.map((item) => {
				// item.=item.cutDownPrice/item.price
				// if (item.createTime) {
				// 	const arr = Vue.prototype.$util.formatTimestamp(item.createTime)
				// 	item.createTime = arr[1] + '月' + arr[2] + '日 ' + arr[3] + ':' + arr[4];
				// }
			})
			commit(types.bargainList, newList)
			return newList
		} catch (e) {
			console.log('request fail', e);
			return
		}
	},
	[types.bargainRecordList + 'Up']: async ({
		commit,
		state,
		dispatch
	}, list = []) => {
		return commit(types.bargainRecordList, list)
	},
}

const mutations = {
	[types.luckyCodeList]: (state, res) => {
		state[types.luckyCodeList] = res
	},
	[types.participantList]: (state, res) => {
		state[types.participantList] = res
	},
	[types.drawList]: (state, res) => {
		state[types.drawList] = res
	},
	[types.bargainList]: (state, res) => {
		state[types.bargainList] = res
	},
	[types.bargainRecordList]: (state, res) => {
		state[types.bargainRecordList] = res
	},
}

export default {
	namespaced: true,
	types,
	state,
	getters,
	actions,
	mutations
}
